import java.sql.SQLException;

import oracle.jdbc.aq.AQAgent;
import oracle.jdbc.aq.AQDequeueOptions;
import oracle.jdbc.aq.AQEnqueueOptions;
import oracle.jdbc.aq.AQFactory;
import oracle.jdbc.aq.AQMessage;
import oracle.jdbc.aq.AQMessageProperties;
import oracle.jdbc.driver.OracleConnection;

import org.apache.log4j.Logger;


public class AQEnqueue extends OracleAQ {
	
	private Logger logger = Logger.getLogger( AQEnqueue.class);
	
	public AQEnqueue() {}
	
	public AQEnqueue( String user, String password, String host, String port, String sid) {
		super( user, password, host, port, sid);
	
	}
	
	public AQEnqueue( String user, String password, String url) {
		super( user, password, url);
	
	}
	
	public void enqueue( String queueName, AQAgent[] recipients, byte[] rawPayload) throws SQLException {
		
		logger.info( "============= Enqueue start =============");
		
		this.connect();
		
		// First create the message properties;
		AQMessageProperties msgprop = AQFactory.createAQMessageProperties();
		msgprop.setCorrelation( "mycorrelation");
		msgprop.setExceptionQueue( "MY_EXCEPTION_QUEUE");
		
		// Specify an agent as the sender
		AQAgent ag = AQFactory.createAQAgent();
		ag.setName( "MY_SENDER_AGENT_NAME");
		ag.setAddress( "MY_SENDER_AGENT_ADDRESS");
		msgprop.setSender( ag);
		
		// Handle mulit consumer case
		if( recipients != null) {
			msgprop.setRecipientList( recipients);
		}
		
		logger.info( msgprop.toString());
		
		// Create the actual AQMessage instance
		AQMessage msg = AQFactory.createAQMessage( msgprop);
		// and add a payload

		logger.info( "rawPayload = " + new String( rawPayload));
		
		msg.setPayload( new oracle.sql.RAW( rawPayload));
		
		// We want to retrieve the message id after enqueue
		AQEnqueueOptions opt = new AQEnqueueOptions();
		opt.setRetrieveMessageId( true);
		
		// Execute the actual enqueue operation
		this.conn.enqueue( queueName, opt, msg);
		
		byte[] msgId = msg.getMessageId();
		
		if( msgId != null) {
			String msgIdStr = AQByteUtil.byteBufferToHexString( msgId, 20);
			logger.info( "Message ID from enqueue call : " + msgIdStr);
		}
		
		logger.info( "============= Enqueue done =============");
		
	}
	
	public void enqueue( OracleConnection conn, String queueName, AQAgent[] recipients, byte[] rawPayload) throws SQLException {
		
		logger.info( "============= Enqueue start =============");
		
		// First create the message properties;
		AQMessageProperties msgprop = AQFactory.createAQMessageProperties();
		msgprop.setCorrelation( "mycorrelation");
		msgprop.setExceptionQueue( "MY_EXCEPTION_QUEUE");
		
		// Specify an agent as the sender
		AQAgent ag = AQFactory.createAQAgent();
		ag.setName( "MY_SENDER_AGENT_NAME");
		ag.setAddress( "MY_SENDER_AGENT_ADDRESS");
		msgprop.setSender( ag);
		
		// Handle mulit consumer case
		if( recipients != null) {
			msgprop.setRecipientList( recipients);
		}
		
		logger.info( msgprop.toString());
		
		// Create the actual AQMessage instance
		AQMessage msg = AQFactory.createAQMessage( msgprop);
		// and add a payload
		
		logger.info( "rawPayload = " + new String( rawPayload));
		
		msg.setPayload( new oracle.sql.RAW( rawPayload));
		
		
		// We want to retrieve the message id after enqueue
		AQEnqueueOptions opt = new AQEnqueueOptions();
		opt.setRetrieveMessageId( true);
		
		// Execute the actual enqueue operation
		conn.enqueue( queueName, opt, msg);
		
		byte[] msgId = msg.getMessageId();
		
		if( msgId != null) {
			String msgIdStr = AQByteUtil.byteBufferToHexString( msgId, 20);
			logger.info( "Message ID from enqueue call : " + msgIdStr);
		}
		
		logger.info( "============= Enqueue done =============");
		
	}
	
	public void enqueue( String queueName, AQAgent[] recipients, String rawPayload) throws SQLException {
		
		logger.info( "============= Enqueue start =============");
		
		this.connect();
		
		// First create the message properties;
		AQMessageProperties msgprop = AQFactory.createAQMessageProperties();
		msgprop.setCorrelation( "mycorrelation");
		msgprop.setExceptionQueue( "MY_EXCEPTION_QUEUE");
		
		// Specify an agent as the sender
		AQAgent ag = AQFactory.createAQAgent();
		ag.setName( "MY_SENDER_AGENT_NAME");
		ag.setAddress( "MY_SENDER_AGENT_ADDRESS");
		msgprop.setSender( ag);
		
		// Handle mulit consumer case
		if( recipients != null) {
			msgprop.setRecipientList( recipients);
		}
		
		logger.info( msgprop.toString());
		
		// Create the actual AQMessage instance
		AQMessage msg = AQFactory.createAQMessage( msgprop);
		// and add a payload
		
		logger.info( "rawPayload = " + rawPayload);
		
//		msg.setPayload( rawPayload.getBytes());
		msg.setPayload( new oracle.sql.RAW( rawPayload.getBytes()));
		
		// We want to retrieve the message id after enqueue
		AQEnqueueOptions opt = new AQEnqueueOptions();
		opt.setRetrieveMessageId( true);
		AQDequeueOptions dopt = new AQDequeueOptions();
		
		// Execute the actual enqueue operation
		this.conn.enqueue( queueName, opt, msg);
		
		byte[] msgId = msg.getMessageId();
		
		if( msgId != null) {
			String msgIdStr = AQByteUtil.byteBufferToHexString( msgId, 20);
			logger.info( "Message ID from enqueue call : " + msgIdStr);
		}
		
		logger.info( "============= Enqueue done =============");
		
	}
	
	public void enqueue( OracleConnection conn, String queueName, AQAgent[] recipients, String rawPayload) throws SQLException {
		
		logger.info( "============= Enqueue start =============");
		
		// First create the message properties;
		AQMessageProperties msgprop = AQFactory.createAQMessageProperties();
		msgprop.setCorrelation( "mycorrelation");
		msgprop.setExceptionQueue( "MY_EXCEPTION_QUEUE");
		
		// Specify an agent as the sender
		AQAgent ag = AQFactory.createAQAgent();
		ag.setName( "MY_SENDER_AGENT_NAME");
		ag.setAddress( "MY_SENDER_AGENT_ADDRESS");
		msgprop.setSender( ag);
		
		// Handle mulit consumer case
		if( recipients != null) {
			msgprop.setRecipientList( recipients);
		}
		
		logger.info( msgprop.toString());
		
		// Create the actual AQMessage instance
		AQMessage msg = AQFactory.createAQMessage( msgprop);
		// and add a payload
		
		logger.info( "rawPayload = " + rawPayload);
		
		msg.setPayload( new oracle.sql.RAW( rawPayload.getBytes()));
		
		// We want to retrieve the message id after enqueue
		AQEnqueueOptions opt = new AQEnqueueOptions();
		opt.setRetrieveMessageId( true);
		
		// Execute the actual enqueue operation
		conn.enqueue( queueName, opt, msg);
		
		byte[] msgId = msg.getMessageId();
		
		if( msgId != null) {
			String msgIdStr = AQByteUtil.byteBufferToHexString( msgId, 20);
			logger.info( "Message ID from enqueue call : " + msgIdStr);
		}
		
		logger.info( "============= Enqueue done =============");
		
	}
	
	public void enqueue( String queueName, AQAgent[] recipients, byte[] rawPayload, int queueType) throws SQLException {
		
		logger.info( "============= Enqueue start =============");
		
		this.connect();
		
		// First create the message properties;
		AQMessageProperties msgprop = AQFactory.createAQMessageProperties();
		msgprop.setCorrelation( "mycorrelation");
		msgprop.setExceptionQueue( "MY_EXCEPTION_QUEUE");
		
		// Specify an agent as the sender
		AQAgent ag = AQFactory.createAQAgent();
		ag.setName( "MY_SENDER_AGENT_NAME");
		ag.setAddress( "MY_SENDER_AGENT_ADDRESS");
		msgprop.setSender( ag);
		
		// Handle mulit consumer case
		if( recipients != null) {
			msgprop.setRecipientList( recipients);
		}
		
		logger.info( msgprop.toString());
		
		// Create the actual AQMessage instance
		AQMessage msg = AQFactory.createAQMessage( msgprop);
		// and add a payload
		
		logger.info( "rawPayload = " + new String( rawPayload));
		
		if ( queueType == OracleAQ.PAYLOAD_TYPE_RAW) {
			msg.setPayload( new oracle.sql.RAW( rawPayload));
		} else {
			msg.setPayload( rawPayload);
		}
		
		// We want to retrieve the message id after enqueue
		AQEnqueueOptions opt = new AQEnqueueOptions();
		opt.setRetrieveMessageId( true);
		
		// Execute the actual enqueue operation
		this.conn.enqueue( queueName, opt, msg);
		
		byte[] msgId = msg.getMessageId();
		
		if( msgId != null) {
			String msgIdStr = AQByteUtil.byteBufferToHexString( msgId, 20);
			logger.info( "Message ID from enqueue call : " + msgIdStr);
		}
		
		logger.info( "============= Enqueue done =============");
		
	}
	
	public void enqueue( OracleConnection conn, String queueName, AQAgent[] recipients, byte[] rawPayload, int queueType) throws SQLException {
		
		logger.info( "============= Enqueue start =============");
		
		// First create the message properties;
		AQMessageProperties msgprop = AQFactory.createAQMessageProperties();
		msgprop.setCorrelation( "mycorrelation");
		msgprop.setExceptionQueue( "MY_EXCEPTION_QUEUE");
		
		// Specify an agent as the sender
		AQAgent ag = AQFactory.createAQAgent();
		ag.setName( "MY_SENDER_AGENT_NAME");
		ag.setAddress( "MY_SENDER_AGENT_ADDRESS");
		msgprop.setSender( ag);
		
		// Handle mulit consumer case
		if( recipients != null) {
			msgprop.setRecipientList( recipients);
		}
		
		logger.info( msgprop.toString());
		
		// Create the actual AQMessage instance
		AQMessage msg = AQFactory.createAQMessage( msgprop);
		// and add a payload
		
		logger.info( "rawPayload = " + new String( rawPayload));
		
		if ( queueType == OracleAQ.PAYLOAD_TYPE_RAW) {
			msg.setPayload( new oracle.sql.RAW( rawPayload));
		} else {
			msg.setPayload( rawPayload);
		}
		
		// We want to retrieve the message id after enqueue
		AQEnqueueOptions opt = new AQEnqueueOptions();
		opt.setRetrieveMessageId( true);
		
		// Execute the actual enqueue operation
		conn.enqueue( queueName, opt, msg);
		
		byte[] msgId = msg.getMessageId();
		
		if( msgId != null) {
			String msgIdStr = AQByteUtil.byteBufferToHexString( msgId, 20);
			logger.info( "Message ID from enqueue call : " + msgIdStr);
		}
		
		logger.info( "============= Enqueue done =============");
		
	}
	
	public void enqueue( String queueName, AQAgent[] recipients, String rawPayload, int queueType) throws SQLException {
		
		logger.info( "============= Enqueue start =============");
		
		this.connect();
		
		// First create the message properties;
		AQMessageProperties msgprop = AQFactory.createAQMessageProperties();
		msgprop.setCorrelation( "mycorrelation");
		msgprop.setExceptionQueue( "MY_EXCEPTION_QUEUE");
		
		// Specify an agent as the sender
		AQAgent ag = AQFactory.createAQAgent();
		ag.setName( "MY_SENDER_AGENT_NAME");
		ag.setAddress( "MY_SENDER_AGENT_ADDRESS");
		msgprop.setSender( ag);
		
		// Handle mulit consumer case
		if( recipients != null) {
			msgprop.setRecipientList( recipients);
		}
		
		logger.info( msgprop.toString());
		
		// Create the actual AQMessage instance
		AQMessage msg = AQFactory.createAQMessage( msgprop);
		// and add a payload
		
		logger.info( "rawPayload = " + rawPayload);
		
		if ( queueType == OracleAQ.PAYLOAD_TYPE_RAW) {
			msg.setPayload( new oracle.sql.RAW( rawPayload.getBytes()));
		} else {
			msg.setPayload( rawPayload.getBytes());
		}
		
		// We want to retrieve the message id after enqueue
		AQEnqueueOptions opt = new AQEnqueueOptions();
		opt.setRetrieveMessageId( true);
		
		// Execute the actual enqueue operation
		this.conn.enqueue( queueName, opt, msg);
		
		byte[] msgId = msg.getMessageId();
		
		if( msgId != null) {
			String msgIdStr = AQByteUtil.byteBufferToHexString( msgId, 20);
			logger.info( "Message ID from enqueue call : " + msgIdStr);
		}
		
		logger.info( "============= Enqueue done =============");
		
	}

	public void enqueue( OracleConnection conn, String queueName, AQAgent[] recipients, String rawPayload, int queueType) throws SQLException {
		
		logger.info( "============= Enqueue start =============");
		
		// First create the message properties;
		AQMessageProperties msgprop = AQFactory.createAQMessageProperties();
		msgprop.setCorrelation( "mycorrelation");
		msgprop.setExceptionQueue( "MY_EXCEPTION_QUEUE");
		
		// Specify an agent as the sender
		AQAgent ag = AQFactory.createAQAgent();
		ag.setName( "MY_SENDER_AGENT_NAME");
		ag.setAddress( "MY_SENDER_AGENT_ADDRESS");
		msgprop.setSender( ag);
		
		// Handle mulit consumer case
		if( recipients != null) {
			msgprop.setRecipientList( recipients);
		}
		
		logger.info( msgprop.toString());
		
		// Create the actual AQMessage instance
		AQMessage msg = AQFactory.createAQMessage( msgprop);
		// and add a payload
		
		logger.info( "rawPayload = " + rawPayload);
		
		if ( queueType == OracleAQ.PAYLOAD_TYPE_RAW) {
			msg.setPayload( new oracle.sql.RAW( rawPayload.getBytes()));
		} else {
			msg.setPayload( rawPayload.getBytes());
		}
		
		// We want to retrieve the message id after enqueue
		AQEnqueueOptions opt = new AQEnqueueOptions();
		opt.setRetrieveMessageId( true);
		
		// Execute the actual enqueue operation
		conn.enqueue( queueName, opt, msg);
		
		byte[] msgId = msg.getMessageId();
		
		if( msgId != null) {
			String msgIdStr = AQByteUtil.byteBufferToHexString( msgId, 20);
			logger.info( "Message ID from enqueue call : " + msgIdStr);
		}
		
		logger.info( "============= Enqueue done =============");
		
	}
	
}
